// miniprogram/pages/result/result.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:"",
    score:0,
    scoreLeft:"",
    scoreCenter:"",
    scoreRight:"",
    centerShow:"none",
    typeUrl:"",
    wordUrl:"",
    titleUrl:"",
    introUrl:"",
    isPrize:true,
    arr:[{
      "score":60,
      "type":"../../images/result1_type.png",
      "word":"../../images/result1_word.png",
      "title":"../../images/result1_title.png",
      "intro":"../../images/result1_intro.png"
    },
    {
      "score":70,
      "type":"../../images/result2_type.png",
      "word":"../../images/result2_word.png",
      "title":"../../images/result2_title.png",
      "intro":"../../images/result2_intro.png"
    },
    {
      "score":80,
      "type":"../../images/result3_type.png",
      "word":"../../images/result3_word.png",
      "title":"../../images/result3_title.png",
      "intro":"../../images/result3_intro.png"
    },
    {
      "score":90,
      "type":"../../images/result4_type.png",
      "word":"../../images/result4_word.png",
      "title":"../../images/result4_title.png",
      "intro":"../../images/result4_intro.png"
    },
    {
      "score":100,
      "type":"../../images/result5_type.png",
      "word":"../../images/result5_word.png",
      "title":"../../images/result5_title.png",
      "intro":"../../images/result5_intro.png"
    }]
  },
  //渲染分数
  getScore(){
    if(this.data.score == 100){
      this.setData({
        scoreLeft:"../../images/1.png",
        scoreCenter:"../../images/0.png",
        scoreRight:"../../images/0.png",
        centerShow:"block"
      })
    }
    else{
      var that = this;
      var left = Math.floor(this.data.score/10);
      var right = this.data.score%10;
      var i=0,j=0;
      var time = setInterval(function(){
        that.setData({
          scoreLeft:"../../images/"+i+".png",
          scoreRight:"../../images/"+j+".png"
        })
        i++;
        j++;
        if(i>left || j>right){
          clearInterval(time);
          if(i<left){
            var time1 = setInterval(function(){
              that.setData({
                scoreLeft:"../../images/"+i+".png"
              })
              i++;
              if(i>left){
                clearInterval(time1);
              }
            },100)
          }
          if(j<right){
            var time2 = setInterval(function(){
              that.setData({
                scoreLeft:"../../images/"+j+".png"
              })
              j++;
              if(j>right){
                clearInterval(time2);
              }
            },300)
          }
        }
      },300)
    }
  },

  //渲染结果数据
  getData(){
    var that = this;
    var num = Math.ceil(this.data.score/10)*10;
    if(num<60){
      num = 60;
    }
    var arr = this.data.arr;
    for(var i=0;i<arr.length;i++){
      if(num == arr[i].score){
        this.setData({
          typeUrl:arr[i].type,
          wordUrl:arr[i].word,
          titleUrl:arr[i].title,
          introUrl:arr[i].intro
        })
        break;
      }
    }
  },
  giftBtn(){ //点击马上开奖按钮
    var that = this;

    // wx.cloud.callFunction({
    //   name:"getUserID",
    //   success(res){
    //     console.log(111,res);
    //     that.setData({
    //       openid:res.result.res
    //     })
        //判断用户是否已经开过奖
        wx.cloud.callFunction({
          name:"getPrize",
          // data:{
          //   openid:that.data.openid
          // },
          success(res){
            console.log(res);
            if(res.result.res.data.length != 0){
              wx.showToast({
                title: '你已中奖过，无法参与抽奖，谢谢参与',
                icon:"none",
                duration: 5000
              })
            }else{
              //判断用户的抽奖次数
              wx.cloud.callFunction({
                name:"getUserInfoTime",
                success(res){
                  console.log(res);
                  if(res.result.res.data[0].prizeCount < 3){
                    //更改用户玩的次数
                    wx.cloud.callFunction({
                      name:"getUserInfoTime",
                      success(res){
                        console.log(res);
                        var prizeCount = ++res.result.res.data[0].prizeCount;
                        wx.cloud.callFunction({
                          name:"updateUserInfo",
                          data:{
                            id:res.result.res.data[0]._id,
                            prizeCount:prizeCount,
                            gameCount:res.result.res.data[0].gameCount
                          },
                          success(res){
                            console.log(res);
                          },
                          fail(err){
                            console.log(err);
                          }
                        })
                      },
                      fail(err){
                        console.log(err);
                      }
                    })

                    if(that.data.isPrize){
                      that.getPrizeLevel();
                      that.setData({
                        isPrize:false
                      })
                    }
                    else{
                      wx.navigateTo({
                        url:"../prize/prize?prizeNum="+that.data.prizeNum
                      })
                    }
                  }
                  else{
                    wx.showToast({
                      title: '您的抽奖次数已达，期待明天您的参与',
                      icon:"none",
                      duration: 3000
                    })
                    setTimeout(function(){
                      wx.navigateTo({
                        url:"../index/index"
                      })
                    },3000)
                  }
                }
              })

              
            }
          },
          fail(err){
            console.log("失败",err);
          }
        })
  //     },
  //     fail(err){
  //       console.log(222,err);
  //     }
  //   })
  },
  getPrizeLevel(){
    var prizeNum = 0;
    var num = Math.ceil(Math.random()*100);
    console.log(num);
    //数字小于20为三等奖
    if(num<=47){
      prizeNum = 3;
    }

    //数字为10、30、50、70、90或两个数字一样为二等奖
    var left = Math.floor(num/10);
    var right = num%10;
    var arr = [50,60,70,80,90];
    if(arr.indexOf(num) != -1 || left == right){
      prizeNum = 2;
    }

    //数字为100时为一等奖
    if(num == 100){
      prizeNum = 1;
    }
    console.log(prizeNum);

    //跳转到开奖页面
    wx.navigateTo({
      url:"../prize/prize?prizeNum="+prizeNum
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var score = decodeURIComponent(options.beauty);
    console.log(score);
    this.setData({
      score:score
    })
    wx.showLoading({
      title:"加载中",
      mask:true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading();
    this.getScore();
    this.getData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})